#!/bin/bash
source /home/shell/util.sh

if [ -f "/usr/bin/docker" ];then
  printInfo "docker已安装："`docker -v`
  exit;
fi
# 检查防火墙状态
if [ `firewall-cmd --state` != 'running' ];
then
  systemctl start firewalld
else
  printInfo '防火墙运行中...';
fi

# 安装依赖工具
yum -y install yum-utils

# 使用阿里云镜像
mkdir -p /etc/docker
tee /etc/docker/daemon.json<<EOF
{
   "registry-mirrors" : ["https://nwgq096a.mirror.aliyuncs.com","https://0bc1c2bdb880f3630f6fc015c1f33900.mirror.swr.myhuaweicloud.com"],
   "insecure-registries": []
}
EOF

# 调整时区
timedatectl set-timezone Asia/Shanghai
systemctl restart rsyslog
systemctl restart crond

# 阿里云安装源
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 关闭SELinux
setenforce 0
sudo sed -i "s/^SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
# 删除centos自带
yum erase -y podman buildah docker-ce containerd.io

printInfo "=============选择安装版本============="
yum list docker-ce --showduplicates
printInfo "=============选择安装版本============="
printInfo "请输入安装版本[回车默认最新版本]：\c"
read docker_version
docker_install="docker-ce"
if [ -n "$docker_version" ];
then
  docker_install=$docker_install"-"$docker_version
fi

# 安装软件
yum install -y yum-utils device-mapper-persistent-data lvm2 net-tools $docker_install docker-ce-cli containerd.io tc 

# 设置docker驱动Cgroup=>systemd
sed -i "s@ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock@ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock --exec-opt native.cgroupdriver=systemd@g" /usr/lib/systemd/system/docker.service
# 关闭swap
swapoff  -a
sed -ri 's/.*swap.*/#&/' /etc/fstab
# 启动docker服务
systemctl enable --now docker

firewall-addport 2375

# 显示docker信息
printInfo "============docker============"
docker info
dockerv=`docker -v`
printInfo "客户端版本："$dockerv
docker_serv_ver=`docker info|grep "Server Version"|awk '{print $3}'`
printInfo "服务端版本："$docker_serv_ver
printInfo "============docker============"